home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2004 April / Gamestar_61_2004-04_dvdb.iso / DVDStar / Editace / hltp.exe / {app} / Source Code / Zoners Half-Life Tools / common / filelib.cpp < prev    next >
C/C++ Source or Header  |  2002-01-14  |  4KB  |  218 lines

  1. #ifdef HAVE_CONFIG_H
  2. #include "config.h"
  3. #endif
  4.  
  5. #ifdef SYSTEM_WIN32
  6. #include <sys/stat.h>
  7. #include <io.h>
  8. #include <fcntl.h>
  9. #endif
  10.  
  11. #ifdef SYSTEM_POSIX
  12. #ifdef HAVE_SYS_STAT_H
  13. #include <sys/stat.h>
  14. #endif
  15.  
  16. #ifdef HAVE_FCNTL_H
  17. #include <fcntl.h>
  18. #endif
  19.  
  20. #ifdef HAVE_UNISTD_H
  21. #include <unistd.h>
  22. #endif
  23. #endif
  24.  
  25. #include "cmdlib.h"
  26. #include "messages.h"
  27. #include "log.h"
  28. #include "mathtypes.h"
  29. #include "mathlib.h"
  30. #include "blockmem.h"
  31.  
  32. /*
  33.  * ==============
  34.  * getfiletime        
  35.  * ==============
  36.  */
  37.  
  38. time_t          getfiletime(const char* const filename)
  39. {
  40.     time_t          filetime = 0;
  41.     struct stat     filestat;
  42.  
  43.     if (stat(filename, &filestat) == 0)
  44.         filetime = max(filestat.st_mtime, filestat.st_ctime);
  45.  
  46.     return filetime;
  47. }
  48.  
  49. /*
  50.  * ==============      
  51.  * getfilesize
  52.  * ==============
  53.  */
  54. long            getfilesize(const char* const filename)
  55. {
  56.     long            size = 0;
  57.     struct stat     filestat;
  58.  
  59.     if (stat(filename, &filestat) == 0)
  60.         size = filestat.st_size;
  61.  
  62.     return size;
  63. }
  64.  
  65. /*
  66.  * ==============
  67.  * getfiledata
  68.  * ==============
  69.  */
  70. long            getfiledata(const char* const filename, char* buffer, const int buffersize)
  71. {
  72.     long            size = 0;
  73.     int             handle;
  74.     long            start, end;
  75.  
  76.     time(&start);
  77.  
  78.     if ((handle = open(filename, O_RDONLY)) != -1)
  79.     {
  80.         int             bytesread;
  81.  
  82.         Log("%-20s Restoring [%-13s - ", "BuildVisMatrix:", filename);
  83.         while ((bytesread = read(handle, buffer, min(32 * 1024, buffersize - size))) > 0)
  84.         {
  85.             size += bytesread;
  86.             buffer += bytesread;
  87.         }
  88.         close(handle);
  89.         time(&end);
  90.         Log("%10.3fMB] (%d)\n", size / (1024.0 * 1024.0), end - start);
  91.     }
  92.  
  93.     if (buffersize != size)
  94.     {
  95.         Warning("Invalid file [%s] found.  File will be rebuilt!\n", filename);
  96.         unlink(filename);
  97.     }
  98.  
  99.     return size;
  100. }
  101.  
  102. /*
  103.  * ================
  104.  * filelength
  105.  * ================
  106.  */
  107. int             q_filelength(FILE* f)
  108. {
  109.     int             pos;
  110.     int             end;
  111.  
  112.     pos = ftell(f);
  113.     fseek(f, 0, SEEK_END);
  114.     end = ftell(f);
  115.     fseek(f, pos, SEEK_SET);
  116.  
  117.     return end;
  118. }
  119.  
  120. /*
  121.  * ================
  122.  * exists
  123.  * ================
  124.  */
  125. bool            q_exists(const char* const filename)
  126. {
  127.     FILE*           f;
  128.  
  129.     f = fopen(filename, "rb");
  130.  
  131.     if (!f)
  132.     {
  133.         IfDebug(Developer(DEVELOPER_LEVEL_SPAM, "Checking for existance of file %s (failed)\n", filename));
  134.         return false;
  135.     }
  136.     else
  137.     {
  138.         fclose(f);
  139.         IfDebug(Developer(DEVELOPER_LEVEL_SPAM, "Checking for existance of file %s (success)\n", filename));
  140.         return true;
  141.     }
  142. }
  143.  
  144.  
  145.  
  146.  
  147. FILE*           SafeOpenWrite(const char* const filename)
  148. {
  149.     FILE*           f;
  150.  
  151.     f = fopen(filename, "wb");
  152.  
  153.     if (!f)
  154.         Error("Error opening %s: %s", filename, strerror(errno));
  155.  
  156.     return f;
  157. }
  158.  
  159. FILE*           SafeOpenRead(const char* const filename)
  160. {
  161.     FILE*           f;
  162.  
  163.     f = fopen(filename, "rb");
  164.  
  165.     if (!f)
  166.         Error("Error opening %s: %s", filename, strerror(errno));
  167.  
  168.     return f;
  169. }
  170.  
  171. void            SafeRead(FILE* f, void* buffer, int count)
  172. {
  173.     if (fread(buffer, 1, count, f) != (size_t) count)
  174.         Error("File read failure");
  175. }
  176.  
  177. void            SafeWrite(FILE* f, const void* const buffer, int count)
  178. {
  179.     if (fwrite(buffer, 1, count, f) != (size_t) count)
  180.         Error("File read failure");
  181. }
  182.  
  183. /*
  184.  * ==============
  185.  * LoadFile
  186.  * ==============
  187.  */
  188. int             LoadFile(const char* const filename, char** bufferptr)
  189. {
  190.     FILE*           f;
  191.     int             length;
  192.     char*           buffer;
  193.  
  194.     f = SafeOpenRead(filename);
  195.     length = q_filelength(f);
  196.     buffer = (char*)Alloc(length + 1);
  197.     SafeRead(f, buffer, length);
  198.     fclose(f);
  199.  
  200.     *bufferptr = buffer;
  201.     return length;
  202. }
  203.  
  204. /*
  205.  * ==============
  206.  * SaveFile
  207.  * ==============
  208.  */
  209. void            SaveFile(const char* const filename, const void* const buffer, int count)
  210. {
  211.     FILE*           f;
  212.  
  213.     f = SafeOpenWrite(filename);
  214.     SafeWrite(f, buffer, count);
  215.     fclose(f);
  216. }
  217.  
  218.